package org.hawk.gwtppc.webtest.tests.methodsuite;

import java.lang.reflect.Method;

import org.hawk.gwtppc.webtest.common.Asserts;
import org.hawk.gwtppc.webtest.common.Test;
import org.hawk.gwtppc.webtest.tests.mock.SubClassMock2;

public class InvokeTest implements Test {

	public void run() throws Throwable {
		SubClassMock2 mock = new SubClassMock2(0);
		
		Asserts.assertEquals(mock.value, 0);
		Asserts.assertEquals(SubClassMock2.class.getDeclaredMethod("processValue").invoke(mock), null);
		Asserts.assertEquals(mock.value, 1);
		Asserts.assertEquals(SubClassMock2.class.getDeclaredMethod("processValue").invoke(mock), null);
		Asserts.assertEquals(mock.value, 2);
		
		Class<?> cls = SubClassMock2.class;
		Method m1 = cls.getDeclaredMethod("processValue", int.class);
		try {
			Asserts.assertEquals(m1.invoke(mock, 5), 5);
			Asserts.fail();
		} catch (IllegalAccessException ex) {
		}
		Asserts.assertEquals(mock.value, 2);
		m1.setAccessible(true);
		Asserts.assertEquals(m1.invoke(mock, 5), 5);
		Asserts.assertEquals(mock.value, 3);
		Object obj = new Object();
		Method m2 = SubClassMock2.class.getDeclaredMethod("processValue", int.class, Object.class);
		try {
			Asserts.assertEquals(m2.invoke(mock, 5, obj), obj);
			Asserts.fail();
		} catch (IllegalAccessException ex) {
		}
		Asserts.assertEquals(mock.value, 3);
		m2.setAccessible(true);
		Asserts.assertEquals(m2.invoke(mock, 5, obj), obj);
		Asserts.assertEquals(mock.value, 4);
		Method m3 = SubClassMock2.class.getDeclaredMethod("processValue", int.class, Object.class, SubClassMock2.class);
		m3.setAccessible(true);
		Asserts.assertEquals(m3.invoke(mock, 5, obj, mock), mock);
		Asserts.assertEquals(mock.value, 5);
	}

	public String getName() {
		return "invoke(Object, Object...)";
	}

}
